home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / dos / diverses / leda / incl / list.h < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-15  |  9.3 KB  |  298 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  2.1.1                                                 11-15-1991
  4. +
  5. +
  6. +  list.h
  7. +
  8. +
  9. +  Copyright (c) 1991  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15.  
  16. #ifndef LISTH
  17. #define LISTH
  18.  
  19. /*------------------------------------------------------------------------------ 
  20.     list (doubled linked lists)
  21.  
  22.     Stefan Naeher
  23.     FB10 Informatik 
  24.     Universitaet des Saarlandes 
  25.     6600 Saarbruecken
  26.  
  27.     September 1988
  28.     
  29. ------------------------------------------------------------------------------*/
  30.  
  31. #include <LEDA/basic.h>
  32.  
  33. //------------------------------------------------------------------------------
  34. // some declarations
  35. //------------------------------------------------------------------------------
  36.  
  37. class dlist; 
  38. class dlink;
  39.  
  40. typedef dlink* list_item;
  41.  
  42.  
  43. //------------------------------------------------------------------------------
  44. // class dlink (list items)
  45. //------------------------------------------------------------------------------
  46.  
  47. class dlink {
  48.  
  49.   friend class dlist;
  50.  
  51.   dlink* succ;
  52.   dlink* pred;
  53.   ent e;
  54.  
  55.   dlink(ent a, dlink* pre, dlink* suc)
  56.   { 
  57.     e = a;
  58.     succ = suc;
  59.     pred = pre;
  60.   }
  61.  
  62.   OPERATOR_NEW(3)
  63.   OPERATOR_DEL(3)
  64.  
  65. //space: 3 words = 12 bytes
  66. };
  67.  
  68.  
  69. //------------------------------------------------------------------------------
  70. // dlist: base class for all doubly linked lists
  71. //------------------------------------------------------------------------------
  72.  
  73. class dlist {
  74.  
  75.    dlink* h;                     //head
  76.    dlink* t;                     //tail
  77.    dlink* iterator;              //iterator
  78.    int count;                    //length of list
  79.  
  80. //space: 4 words + virtual =  5 words = 20 bytes
  81.  
  82. // virtual int cmp(ent& x, ent& y) { return x-y; }
  83.  
  84. virtual void print_el(ent& x,ostream& out) const { out << int(x); }
  85. virtual void read_el(ent& x,istream& in)   const { in >> *(int*)&x; }
  86. virtual void clear_el(ent& x) const { x=0; }
  87. virtual void copy_el(ent& x)  const { x=x; }
  88. virtual void init_el(ent& x)  const { x=0; }
  89.  
  90. // void quick_sort(list_item* A,int l, int r);
  91.  
  92. void quick_sort(list_item* A,int l, int r, CMP_ENT cmp_fct);
  93.  
  94.  
  95. public:
  96.  
  97. // access operations
  98.  
  99.    int  length() const { return count; }
  100.    int  size()   const { return count; }
  101.    bool empty()  const { return (count==0) ? true : false;}
  102.  
  103.    dlink* first()               const { return h; }
  104.    dlink* first_item()          const { return h; }
  105.    dlink* last()                const { return t; }
  106.    dlink* last_item()           const { return t; }
  107.    dlink* next_item(dlink* p)   const { return p->succ; }
  108.    dlink* succ(dlink* l)        const { return l->succ; }
  109.    dlink* pred(dlink* l)        const { return l->pred; }
  110.    dlink* cyclic_succ(dlink*)   const;
  111.    dlink* cyclic_pred(dlink*)   const;
  112.    dlink* succ(dlink* l, int i) const; 
  113.    dlink* pred(dlink* l, int i) const;
  114.    dlink* get_item(int = 0)     const; 
  115.  
  116.    dlink* max(CMP_ENT) const;
  117.    dlink* min(CMP_ENT) const;
  118.    dlink* search(ent)  const;
  119.  
  120.    int    rank(ent) const;
  121.  
  122.    ent    contents(dlink* l) const { return l->e; }
  123.    ent    head()             const { return h ? h->e : 0;}
  124.    ent    tail()             const { return t ? t->e : 0;}
  125.  
  126.  
  127. // update operations
  128.  
  129.    dlink* insert(ent a, dlink* l, int dir=0);
  130.    dlink* push(ent a);
  131.    dlink* append(ent a);
  132.  
  133.    void   assign(dlink* l, ent a) { copy_el(a); l->e = a; }
  134.    void   conc(dlist&);
  135.    void   split(list_item,dlist&,dlist&);
  136.  
  137.    void   del(dlink* loc);
  138.    void   pop();
  139.    void   Pop();
  140.  
  141.    void   apply(ENT_TO_VOID);
  142.    void   sort(CMP_ENT);
  143.    void   bucket_sort(int i, int j, ENT_TO_INT f);
  144.    void   permute();
  145.    void   clear();
  146.  
  147. // iteration
  148.  
  149.    void   set_iterator(dlink* loc) const     
  150.    { const list_item *const p = &iterator;    // hack to stay const
  151.      (*(dlink**)p) = loc;
  152.     }
  153.  
  154.    void  init_iterator()        const { set_iterator(nil); }
  155.    void  reset()                const { set_iterator(nil); }
  156.    dlink* get_iterator()        const { return iterator; }
  157.  
  158.    dlink* move_iterator(int=0)  const;
  159.    bool   current_element(ent&) const;
  160.    bool   next_element(ent&)    const;
  161.    bool   prev_element(ent&)    const;
  162.  
  163.  
  164. // operators
  165.  
  166.    ent&   entry(dlink* l)            { return l->e; }
  167.    ent    inf(dlink* l)        const { return l->e; }
  168.    ent&   operator[](dlink* l)       { return l->e; }
  169.    ent    operator[](dlink* l) const { return l->e; }
  170.  
  171.    dlist& operator=(const dlist&); 
  172.    dlist  operator+(const dlist&); 
  173.  
  174.  
  175.    void print(ostream&,string, char)       const;    
  176.    void print(ostream& out,char space=' ') const { print(out,"",space);  }
  177.    void print(string s, char space=' ')    const { print(cout,s,space);  }
  178.    void print(char space=' ')              const { print(cout,"",space); }   
  179.  
  180.  
  181.    void read(istream&,string, char);  
  182.    void read(istream& in,char delim=EOF) { read(in,"",delim); }
  183.    void read(string s, char delim='\n')  { read(cin,s,delim); }   
  184.    void read(char delim='\n')            { read(cin,"",delim);}  
  185.  
  186.  
  187. // constructors & destructors
  188.  
  189.    dlist();    
  190.    dlist(ent a);
  191.    dlist(const dlist&);
  192.  
  193.   ~dlist()  { clear(); }
  194.  
  195.    int space()  const { return sizeof(dlist) + count * sizeof(dlink); }
  196. };
  197.  
  198.  
  199. //------------------------------------------------------------------------------
  200. // list: generic dlists
  201. //-----------------------------------------------------------------------------
  202.  
  203. #define list(type) name2(type,list)
  204.  
  205. #define listdeclare(type)\
  206. \
  207. typedef int  (*COMPARE(type,list)) (type&, type&);\
  208. typedef void (*APPLY(type,list))   (type&);\
  209. typedef int  (*ORD(type,list))     (type&);\
  210. \
  211. struct list(type) : public dlist {\
  212. \
  213. void print_el(ent& x,ostream& out) const { Print(*(type*)&x,out);  }\
  214. void read_el(ent& x,istream& in)   const { type y=0; Read(y,in); x = Copy(y); }\
  215. void clear_el(ent& x) const { Clear(*(type*)&x); }\
  216. void copy_el(ent& x)  const { Copy(*(type*)&x);  }\
  217. void init_el(ent& x)  const { type y=0; x = Copy(y);  }\
  218. \
  219. void   conc(list(type)& l)    { dlist::conc((dlist&)l); }\
  220. void   split(list_item p, list(type)& l1, list(type)& l2)\
  221.                               { dlist::split(p,(dlist&)l1,(dlist&)l2);}\
  222. list_item search(type a) const{ return dlist::search(Ent(a)); }\
  223. int       rank(type a)   const{ return dlist::rank(Ent(a)); }\
  224. \
  225. list_item push(type a)        { return dlist::push(Copy(a)); }\
  226. list_item append(type a)      { return dlist::append(Copy(a)); }\
  227. list_item insert(type a, list_item l, int dir=0)\
  228.                               { return dlist::insert(Copy(a),l,dir); }\
  229. \
  230. type  contents(list_item l) const { ent x = dlist::contents(l); return *(type*)&x; }\
  231. type  inf(list_item l)   const{ return contents(l); }\
  232. \
  233. type  head()             const{ ent x = dlist::head(); return *(type*)&x;    }\
  234. type  tail()             const{ ent x = dlist::tail(); return *(type*)&x;    }\
  235. \
  236. type  pop()                   { type x=head(); dlist::pop(); return x;       }\
  237. type  Pop()                   { type x=tail(); dlist::Pop(); return x;       }\
  238. \
  239. type  del(list_item a)        { type x=contents(a); dlist::del(a); return x; }\
  240. type  del_item(list_item a)   { type x=contents(a); dlist::del(a); return x; }\
  241. \
  242. void   assign(list_item p,type a) { dlist::assign(p,Ent(a)); }\
  243. \
  244. list_item max(COMPARE(type,list) f=0) const { return dlist::max(CMP_ENT(f)); }\
  245. list_item min(COMPARE(type,list) f=0) const { return dlist::max(CMP_ENT(f)); }\
  246. void      sort(COMPARE(type,list) f=0) { dlist::sort(CMP_ENT(f)); }\
  247. void      apply(APPLY(type,list) f)    { dlist::apply(ENT_TO_VOID(f)); }\
  248. void      bucket_sort(int i, int j, ORD(type,list) f)\
  249.                                   { dlist::bucket_sort(i,j,ENT_TO_INT(f)); }\
  250. \
  251. bool current_element(type& x) const { ent y; bool b = dlist::current_element(y);\
  252.                                       if (b) x = *(type*)&y; return b; }\
  253. bool next_element(type& x)    const { ent y; bool b = dlist::next_element(y);\
  254.                                       if (b) x = *(type*)&y; return b; }\
  255. bool prev_element(type& x)    const { ent y; bool b = dlist::prev_element(y);\
  256.                                       if (b) x = *(type*)&y; return b; }\
  257. \
  258. list(type)() {}\
  259. list(type)(const list(type)& a) : dlist((dlist&)a) {}\
  260. list(type)(type a)              : dlist(Ent(a))    {}\
  261. \
  262. ~list(type)() { clear(); }\
  263. \
  264. list(type)& operator=(const list(type)& a)\
  265.                             { return *(list(type)*)&dlist::operator=(a);}\
  266. \
  267. list(type) operator+(const list(type)& a)\
  268.                             { dlist L = *(dlist*)this + *(dlist*)&a;\
  269.                            /* dlist L = dlist::operator+(a); */     \
  270.                               return *(list(type)*)&L; }\
  271. list_item operator+=(type x)\
  272.                             { return append(x); }\
  273. \
  274. list_item operator[](int i) { return get_item(i); }\
  275. \
  276. type&  operator[](list_item p)\
  277.                             { return *(type*)&dlist::entry(p); }\
  278. type   operator[](list_item p) const\
  279.                             { return type(dlist::inf(p)); }\
  280. }; 
  281.  
  282.  
  283.  
  284. //------------------------------------------------------------------------------
  285. // Iteration Macros
  286. //------------------------------------------------------------------------------
  287.  
  288. #define forall_list_items(a,b) for( a=(b).first(); a ; a=(b).succ(a) )
  289. #define Forall_list_items(a,b) for( a=(b).last();  a ; a=(b).pred(a) )
  290.  
  291. /* forall(a,b)  defined in basic.h */
  292.  
  293. #define Forall(a,b) for( (b).init_iterator(); (b).prev_element(a); )
  294.  
  295.  
  296. #endif
  297.